package com.ycorn.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: wujianmin
 * @Date: 2020/4/7 10:06
 * @Function:
 * @Version 1.0
 */
@RestController
public class TestController {

    @RequestMapping(value = "/r/r1")
    public String r1() {
        return getLoginUserName() + " 成功跳转R1";
    }

    @RequestMapping(value = "/r/r2")
    public String r2() {
        return getLoginUserName() + " 成功跳转R2";
    }

    @RequestMapping(value = "/r/r3")
    @PreAuthorize(value = "hasAnyAuthority('r3')")
    public String r3() {
        return getLoginUserName() + " 成功跳转R3";
    }

    @RequestMapping(value = "/r/r4")
    @PreAuthorize(value = "hasAnyAuthority('r4')")
    public String r4() {
        return getLoginUserName() + " 成功跳转R4";
    }

    private String getLoginUserName() {
        return ((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
    }
}
